//
//  SEGTarget_Share.m
//  SEGStandard
//
//  Created by 山刘 on 2019/4/12.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "SEGTarget_Share.h"
#import "SEGShareViewController.h"
#import "SEGShareHelpCardModel.h"

@implementation SEGTarget_Share
-(UIViewController*)segAction_initShareViewController:(NSDictionary*)params{
    
    SEGShareVCType  shareVCType = [[params allKeys] containsObject:@"shareVCType"] ? (SEGShareVCType)[[params valueForKey:@"shareVCType"] integerValue] : SEGShareVCType_Default;
    SEGShareVCViewType  shareVCViewType = [[params allKeys] containsObject:@"shareVCViewType"] ? (SEGShareVCViewType)[[params valueForKey:@"shareVCViewType"] integerValue] : SEGShareVCViewType_Default;
    NSString *sharetitle = [[params allKeys] containsObject:@"sharetitle"] ? [params valueForKey:@"sharetitle"] : @"";
    NSString *sharecontent = [[params allKeys] containsObject:@"sharecontent"] ? [params valueForKey:@"sharecontent"] : @"";
    NSString *inviteurl = [[params allKeys] containsObject:@"inviteurl"] ? [params valueForKey:@"inviteurl"] : @"";
    NSString *objID = [[params allKeys] containsObject:@"objID"] ? [params valueForKey:@"objID"] : @"";
    NSString *objSection = [[params allKeys] containsObject:@"objSection"] ? [params valueForKey:@"objSection"] : @"";
    
    id delegate = [[params allKeys] containsObject:@"delegate"] ? [params valueForKey:@"delegate"] : nil;
    id actionTypeBlock = [[params allKeys] containsObject:@"actionTypeBlock"] ? [params valueForKey:@"actionTypeBlock"] : nil;
    id channelBlock = [[params allKeys] containsObject:@"channelBlock"] ? [params valueForKey:@"channelBlock"] : nil;
    
    SEGShareViewModel *viewModel = [[SEGShareViewModel alloc] init];
    viewModel.shareVCViewType = shareVCViewType;
    viewModel.shareVCType = shareVCType;
    viewModel.objID = objID;
    viewModel.objSection = objSection;
    if ([[params allKeys] containsObject:@"miniapp"] &&
        [[params valueForKey:@"miniapp"] isKindOfClass:[NSDictionary class]]) {
        viewModel.miniapp = [params valueForKey:@"miniapp"];
    }
    
    if ([[params allKeys] containsObject:@"helpCardModel"] &&
        [[params valueForKey:@"helpCardModel"] isKindOfClass:[SEGShareHelpCardModel class]]) {
        viewModel.helpCardModel = [params valueForKey:@"helpCardModel"];
    }
    
    if ([[params allKeys] containsObject:@"hdImage"] &&
        [[params valueForKey:@"hdImage"] isKindOfClass:[UIImage class]]) {
        viewModel.hdImage = [params valueForKey:@"hdImage"];
    }
    
    if ([[params allKeys] containsObject:@"shareImage"] &&
        [[params valueForKey:@"shareImage"] isKindOfClass:[UIImage class]]) {
        viewModel.shareImage = [params valueForKey:@"shareImage"];
    }else{
        viewModel.shareImageUrlString = [[params allKeys] containsObject:@"shareImageUrlString"] ? [params valueForKey:@"shareImageUrlString"] : @"";
    }
    
    viewModel.shareTitle = [sharetitle length] > 0 ? sharetitle : kAppName;
    viewModel.shareText = [sharecontent length] > 0 ? sharecontent : kAppName;
    viewModel.shareUrlString = inviteurl ? [SEGPlatformManager getUrlStringWithAppPlatformCode:inviteurl] : @"";
    
    if ([[params allKeys] containsObject:@"otherActionTypesArr"]) {
        viewModel.otherActionTypesArr = [params valueForKey:@"otherActionTypesArr"];
    }
    [viewModel shareIconsDataSource:[params valueForKey:@"shareTypesArr"]
                          shareTypesArrType:[params valueForKey:@"shareTypesArrType"]];
    
    SEGShareViewController* shareVC = [[SEGShareViewController alloc] initWithViewModel:viewModel];
    shareVC.delegate = delegate;
    shareVC.actionTypeBlock = actionTypeBlock;
    shareVC.channelBlock = channelBlock;
    
    if (@available(iOS 8.0, *)) {
        shareVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    else {
        shareVC.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    
    return shareVC;
}
@end

